﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;

namespace com.vs.Shipping.BL
{
    public class ShippingLoadManager
    {
        public string ConfirmLoad(Session sess, int iIntLoadNum)
        {
            DataHelper helper = null;
            string stErr = null;

            try
            {
                //判断状态是否可以发车
                helper = new DataHelper(sess);
                stErr = helper.GetString(CommandType.StoredProcedure, "SHP_VConfirmLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)});
                if (!StringManager.IsEmpty(stErr))
                {
                    return stErr;
                }

                #region 扣除库存，更新状态，归档任务
                stErr = helper.GetString(System.Data.CommandType.StoredProcedure, "SHP_ConfirmLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)});
                #endregion

                return stErr;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string CloseLoad(Session sess, int iIntLoadNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(CommandType.StoredProcedure, "SHP_CloseLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)
                });
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string CreateWaveLoad(Session sess, int iWaveNum)
        {
            return null;
        }

        public int GetMinTrailStatus(Session sess, int iIntLoadNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetInt(CommandType.StoredProcedure, "SHP_RMinTrailStatus", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return 0;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public int GetMaxLeadStatus(Session sess, int iIntLoadNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetInt(CommandType.StoredProcedure, "SHP_RMaxLeadStatus", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return 0;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string UpdateLoadStatus(Session sess, int iIntLoadNum)
        {
            ShippingLoadDO slDO = null;
            int iLeadSts = 0;
            int iTrailSts = 0;
            Dictionary<string, object> dcValue = null;

            try
            {
                slDO = new ShippingLoadDO();
                iLeadSts = this.GetMaxLeadStatus(sess, iIntLoadNum);
                iTrailSts = this.GetMinTrailStatus(sess, iIntLoadNum);
                dcValue = new Dictionary<string,object>();
                dcValue.Add("LEADING_STS", iLeadSts);
                dcValue.Add("TRAILING_STS", iTrailSts);
                slDO.Update(sess, dcValue);
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }
    }
}
